//订单评论
'use strict';
import React,{Component} from 'react';
import styles from '../style/styles';
import Header from '../common/header';
import StarRating from 'react-native-star-rating';
import ImagePicker from 'react-native-image-picker';
import Icon from 'react-native-vector-icons';
import * as URLconstant from '../constant/URLconstant';
import Util from '../common/util'
import Toast from '../common/ToastUtil'
import {
    View,
    Text,
    Image,
    TouchableOpacity,
    ScrollView,
    TextInput,
    Platform
} from 'react-native';
export default class commentOrder extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
          this.postPhoto=this.postPhoto.bind(this)
          this.onStarRatingPress=this.onStarRatingPress.bind(this)
          this.goUpdataPhoto=this.goUpdataPhoto.bind(this)
          this.imagePicker=this.imagePicker.bind(this)
          this.errorcallback=this.errorcallback.bind(this)
        this.state = {
            starNum:5,
            uploadImgNum:1,
            order_id:this.props.order_id,
            goods_id:this.props.goods_id,
            goods_image:this.props.goods_image,
            user_id:this.props.user_id,
            maininput:''
        };
      }

    onStarRatingPress(rating){
        this.setState({
            starNum:rating
        })
    }

    getImageNameByIOS(imageUri) {
        if (imageUri != "") {
            var names = imageUri.split("/");
            return names[names.length - 1];
        }
    }

    //更改头像
    imagePicker(i){
        let that = this;

        const options = {
            title: '选择一张图片',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍照',
            chooseFromLibraryButtonTitle: '从相册中选择',
            quality: 1.0,
            maxWidth: 500,
            maxHeight: 500,
            storageOptions: {
                skipBackup: true
            }
        };
        ImagePicker.showImagePicker(options, (response)=> {
            if (response.didCancel) {

            }
            else if (response.error) {

            }
            else if (response.customButton) {
            }
            else {
                let source;
                let imageName;
                let imagePath;

                if (Platform.OS === 'android') {
                    source = response.uri;
                    imagePath = response.path;
                    imageName = response.fileName;

                } else {
                    //{"uri":"/var/mobile/Containers/Data/Application/CC01D123-59FA-4205-8B27-C3181596AA5C/Documents/6131CE7A-FD8C-44DB-B9B9-82C322294B88.jpg","isStatic":true}
                    //file:///Users/dev/Library/Developer/CoreSimulator/Devices/DA8C975D-D476-462B-B387-D43FBC55C333/data/Containers/Data/Application/6231A942-22AA-40DC-A303-898EDE2C7533/Documents/2E98BCB0-8A9E-43FF-88E6-E7BE2094EE62.jpg
                    source = response.uri.replace('file://', '');
                    imagePath = response.uri.replace('file://', '');
                    imageName = that.getImageNameByIOS(response.uri);
                }

                let imageinfo = [];
                let imgUri = 'imgUri'+i;
                imageinfo['uri'] = source;//这个是给React-native Image组件显示图片用得地址;
                imageinfo['imageName'] = imageName;//图片名称
                imageinfo['imagePath'] = imagePath;//图片在本机的路径.
                that.setState({
                    [imgUri]: imageinfo,
                    default_image: imageinfo,
                });
            }
        })
    }


    goUpdataPhoto(user_id) {

        let that = this;
        //LOG(user_id)
        let order_id = this.state.order_id
        let goods_id = this.state.goods_id
        let starNum=this.state.starNum;
        let maininput=this.state.maininput
        let das = new FormData();
        if(that.state.default_image==null||that.state.default_image==''){
            das.append("user_id", this.props.user_id);
            das.append("goods_id", goods_id);
            das.append("order_id", order_id);
            das.append('rank',starNum);
            das.append('title',maininput);
            das.append("signme", 1);
        }else{
            let default_image = {
                uri: that.state.default_image['uri'],
                type: "multipart/form-data",
                name: that.state.default_image['imageName']
            };

            das.append("picture", default_image);
            das.append("user_id", this.props.user_id);
            das.append("goods_id", goods_id);
            das.append("order_id", order_id);
            das.append('rank',starNum);
            das.append('title',maininput);
            das.append("signme", 1);


        }

        let url = URLconstant.COMMENTOUDER_URL;
        Util.post2(url, das, this.postPhoto, this.errorcallback)
    }


    postPhoto(datas){
        //LOG(datas)
        if(datas.status==0){
            Toast.show(datas.msg)
            this.props.Refresh1();
            this.props.navigator.pop();
        }else {
            Toast.show(datas.msg)
        }
    }

    errorcallback(err){
        LOG(err)
    }



    uploadImg(){
        let that = this;
        let uploadImgNum = that.state.uploadImgNum;

        let rows;
        let list=[];
        for(let i =1;i<=uploadImgNum;i++){
            //LOG('state==='+JSON.stringify(this.state))
            let imgUri='imgUri'+i;
            let imgUriList = that.state[imgUri]
            let uri;
            if(imgUriList!=null){
                uri=imgUriList['uri'];
            }

            rows=(
                <TouchableOpacity style={styles.comment_used_wrap} onPress={()=>this.imagePicker(i)} key={i}>
                    <Icon name={'md-images'} size={70} color={'#aaa'}/>
                    <Image
                        resizeMode="cover"
                        style={styles.comment_used_img}
                        source={{uri:uri}}/>
                </TouchableOpacity>
            )
            list.push(rows)
        }
        return list;
    }

    render(){
        let user_id = this.props.user_id;
        return(
            <View style={{flex:1}}>
                <Header navigator={this.props.navigator}  initObj={{title:'评论晒单', id:2}}/>
                <View style={styles.order_comment_wrap}>
                    <Image
                        resizeMode="stretch"
                        style={styles.order_goods_img}
                        source={{uri:this.state.goods_image}}/>
                    <View>
                        <Text style={styles.score}>评分</Text>
                        <StarRating
                            disabled={false}
                            maxStars={5}
                            rating={this.state.starNum}
                            starSize={25}
                            emptyStarColor={'#ccc'}
                            starColor={'#f00'}
                            selectedStar={(rating) => this.onStarRatingPress(rating)}
                        />
                    </View>
                </View>
                <View>
                    <TextInput
                        style={styles.comment_input}
                        placeholder={'写下购买体会和使用感受来帮助其他小伙伴~'}
                        multiline={true}
                        underlineColorAndroid={'transparent'}
                        clearButtonMode={'always'}
                        onChangeText={(text)=>this.setState({maininput:text})}
                    />
                </View>
                <View style={styles.comment_upload_wrap}>
                    {this.uploadImg()}
                </View>
                <View style={styles.comment_submit_wrap} >
                    <TouchableOpacity style={styles.comment_submit} onPress={()=>this.goUpdataPhoto(user_id)}>
                        <Text style={styles.submit_txt}>提交</Text>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }
}